package com.igoal.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.Role;

@Repository("roleDAO")
public class RoleDAOImpl extends GenericDaoHibernate<Role, Long> implements RoleDAO {
	public static final String ROLE_USER = "ROLE_USER";
	public static final String ROLE_ADMIN = "ROLE_ADMIN";
	public static final String ROLE_ANONYMOUS = "ROLE_ANONYMOUS";
	
	public RoleDAOImpl() {
		super(Role.class);
	}

	public Role getRoleUser() {
		return getRoleByName(ROLE_USER);
	}
	
	public Role getRoleAdmin(){
		return getRoleByName(ROLE_ADMIN);
	}
	
	public Role getRoleByName(String name) {
		DetachedCriteria dc = DetachedCriteria.forClass(Role.class);
		dc.add(Restrictions.eq("name", name));
		
		List<Role> roles = findByCriteria(dc, 0, 1);
		
		if(roles.size() == 1) return roles.get(0);
		
		return null;
	}
}
